﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using HRM.DAL.DAO;

namespace HRM.DAL.Gateway
{
    public class PhotoGateway:DBGateway
    {
        public bool HasPhotoAlreadyExist(int empId)
        {
            try 
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblPhotos where EmployeeID='"+empId+"'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
            return false;
        }

        public bool SavePhoto(int empId, Photo photo)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("insert into tblPhotos values(@file,@emp)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@file", SqlDbType.VarChar).Value = photo.FileName;
                CommandObj.Parameters.Add("@emp", SqlDbType.Int).Value = empId;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public bool UpdatePhoto(int empId, Photo photo)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = string.Format("update tblPhotos set FileName=@file where EmployeeID='"+empId+"'");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@file", SqlDbType.VarChar).Value = photo.FileName;
                CommandObj.ExecuteNonQuery();
                return true;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }

        public Photo GetPhoto(int empId)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = @"select * from tblPhotos where EmployeeID='" + empId + "'";
                SqlDataReader reader = CommandObj.ExecuteReader();
                Photo photo=new Photo();
                while (reader.Read())
                {
                    photo.Id = Convert.ToInt32(reader[0]);
                    photo.FileName = reader[1].ToString();
                }
                return photo;
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}